//
//  XS_QuestionVC.swift
//  XueDao
//
//  Created by 韩云智 on 2020/12/4.
//

import UIKit
import RxSwift
import RxCocoa

class XS_SelectIssue: XS_BaseModel {
    var id = 0
    var industryName = ""
    var name = ""
}

class XS_QuestionVC: XS_BaseVC {
    struct NextDetail {
        var title: String
        var text: String
        var desc: String
        var next: XS_QuestionVC.Type
    }
    lazy var text = ""
    lazy var desc = ""
    lazy var save: [XS_SelectIssue] = []
    var currect: XS_SelectIssue!
    
    lazy var contentV: UIView = {
       let contentVi = UIView()
        contentVi.backgroundColor = .white
        return contentVi
    }()
    
    override func upView() {
        let tLab = UILabel.xsLabel(size: .scale*20, fontName: .semibold, color: #colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1), text: text)
        view.addSubview(tLab)
        tLab.snp.makeConstraints { m in
            m.left.equalTo(.scale*16)
            m.top.equalTo(.scale*24)
        }
        let dLab = UILabel.xsLabel(size: .scale*14, fontName: .regular, color: #colorLiteral(red: 0.6666666667, green: 0.6941176471, blue: 0.7176470588, alpha: 1), text: desc)
        view.addSubview(dLab)
        dLab.snp.makeConstraints { m in
            m.left.equalTo(.scale*16)
            m.top.equalTo(tLab.snp.bottom).offset(.scale*10)
        }
        
        let scollV = UIScrollView()
        scollV.bounces = false
        view.addSubview(scollV)
        scollV.snp.makeConstraints { (make) in
            make.top.equalTo(dLab.snp.bottom).offset(.scale * 48)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(-.scale * 96)
        }
        scollV.addSubview(contentV)
        contentV.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }
    }
    
    func getBtn(_ item: XS_SelectIssue) -> UIButton {
        let btn = XS_NoHighlightBtn()
        btn.setTitle(item.industryName+item.name, for: .normal)
        btn.setTitleColor(#colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1), for: .normal)
        btn.setTitleColor(#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), for: .selected)
        btn.titleLabel?.font = .xsFont(name: .medium, .scale*16)
        btn.layer.cornerRadius = .scale*24
        btn.layer.backgroundColor = #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
        btn.rx.tap
            .subscribe(onNext: { [weak self] in
                self?.currect = item
            }).disposed(by: disposeBag)
        return btn
    }
    func upButtonObservables(_ btns: [UIButton]) {
        guard btns.count > 0 else { return }
        let observables = btns.map { button in button.rx.tap.map { button } }
        let selected = Observable.from(observables).merge().startWith(btns[0])
        for button in btns {
            selected.map ({ item in
                let set = item == button
                button.layer.backgroundColor = set ? #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1) : #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
                return set
            }).bind(to: button.rx.isSelected)
            .disposed(by: disposeBag)
        }
    }
    
    func getNext(_ next: NextDetail) -> UIButton {
        let text = NSMutableAttributedString()
        let text1 = NSMutableAttributedString(string: "下一步 ")
        text1.yy_color = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        text1.yy_font = .xsFont(name: .medium, .scale*16)
        text.append(text1)
        let attch = NSTextAttachment()
        attch.image = UIImage(named: "编组")
        attch.bounds = CGRect(x: 0, y: 0, width: .scale*20, height: .scale*20)
        let text2 = NSMutableAttributedString(attachment: attch)
        text2.yy_baselineOffset = NSNumber(value: Double(-.scale*4))
        text.append(text2)
        
        let btn = XS_NoHighlightBtn()
        btn.setAttributedTitle(text, for: .normal)
        btn.layer.cornerRadius = .scale*24
        btn.layer.backgroundColor = #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)
        
        btn.rx.tap.asDriver()
            .drive(onNext: { [weak self] in
                guard let `self` = self, let currect = self.currect else { return }
                let vc = next.next.init()
                vc.title = next.title
                vc.text = next.text
                vc.desc = next.desc
                vc.save = self.save + [currect]
                self.navigationController?.pushViewController(vc, animated: true)
            }).disposed(by: disposeBag)
        return btn
    }
    
    func getPrevious() -> UIButton {
        let text = NSMutableAttributedString()
        let attch = NSTextAttachment()
        attch.image = UIImage(named: "编组_1")
        attch.bounds = CGRect(x: 0, y: 0, width: .scale*20, height: .scale*20)
        let text2 = NSMutableAttributedString(attachment: attch)
        text2.yy_baselineOffset = NSNumber(value: Double(-.scale*4))
        text.append(text2)
        let text1 = NSMutableAttributedString(string: " 上一步")
        text1.yy_color = #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)
        text1.yy_font = .xsFont(name: .medium, .scale*16)
        text.append(text1)
        
        let btn = XS_NoHighlightBtn()
        btn.setAttributedTitle(text, for: .normal)
        btn.layer.cornerRadius = .scale*24
        btn.layer.backgroundColor = #colorLiteral(red: 0.2352941176, green: 0.8156862745, blue: 0.6509803922, alpha: 0.1)
        btn.layer.borderWidth = .scale
        btn.layer.borderColor = #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)
        
        btn.rx.tap.asDriver()
            .drive(onNext: { [weak self] in
                self?.navigationController?.popViewController(animated: true)
            }).disposed(by: disposeBag)
        return btn
    }
}

class XS_Q1VC: XS_QuestionVC {
    override func upView() {
        super.upView()
        navigationItem.leftBarButtonItem = UIBarButtonItem.init(image: UIImage.init(named: "")?.withRenderingMode(.alwaysOriginal), style: .plain, target: nil, action: nil)
//        let next = getNext(XS_QuestionVC.NextDetail(title: "问题2，共4个", text: "您现在的专业等级是什么？", desc: "请回答下列问题，方便我们为您量身定制课程计划。", next: XS_Q2VC.self))
        let next = getNext(XS_QuestionVC.NextDetail(title: "问题2，共3个", text: "您的收入是什么？", desc: "请回答下列问题，方便我们为您量身定制课程计划。", next: XS_Q3VC.self))
        view.addSubview(next)
        next.snp.makeConstraints { m in
            m.height.equalTo(.scale*48)
            m.bottom.equalTo(-.scale*32)
            m.left.equalTo(.scale*48)
            m.right.equalTo(-.scale*48)
        }
        
        XS_NetWork.requestJson("/industry/selectIssue")
            .mapModels(type: XS_SelectIssue.self)
            .subscribe(onNext: { [weak self] models, resp in
                guard let `self` = self, resp.success else { return }
                var btns: [UIButton] = []
                for (index, item) in models.enumerated() {
                    let btn = self.getBtn(item)
                    self.contentV.addSubview(btn)
                    btn.snp.makeConstraints { (make) in
                        make.top.equalTo(.scale * CGFloat(index / 2 * (48 + 16)))
                        make.left.equalTo(.scale * CGFloat(index % 2 * (164 + 15) + 16))
                        make.width.equalTo(.scale * 164)
                        make.height.equalTo(.scale * 48)
                        if index == models.count - 1 {
                            make.bottom.equalToSuperview().offset(-.scale * 24)
                        }
                    }
                    btns.append(btn)
                }
                self.upButtonObservables(btns)
                self.currect = models.first
            }).disposed(by: disposeBag)
        
//        let arr = ["会计", "新媒体主播", "专升本", "本生研", "IT培训", "母婴护理"]
        
    }
}

class XS_Q2VC: XS_QuestionVC {
    override func upView() {
        super.upView()
        let prve = getPrevious()
        view.addSubview(prve)
        prve.snp.makeConstraints { m in
            m.height.equalTo(.scale*48)
            m.bottom.equalTo(-.scale*32)
            m.left.equalTo(.scale*16)
            m.width.equalTo(.scale*120)
        }
        let next = getNext(XS_QuestionVC.NextDetail(title: "问题3，共4个", text: "您的收入是什么？", desc: "请回答下列问题，方便我们为您量身定制课程计划。", next: XS_Q3VC.self))
        view.addSubview(next)
        next.snp.makeConstraints { m in
            m.height.equalTo(.scale*48)
            m.bottom.equalTo(-.scale*32)
            m.left.equalTo(prve.snp.right).offset(.scale*16)
            m.right.equalTo(-.scale*16)
        }
        
        XS_NetWork.requestJson("/industry/queryCertificate", parameters: ["id":save[0].id])
            .mapModels(type: XS_SelectIssue.self)
            .subscribe(onNext: { [weak self] models, resp in
                guard let `self` = self, resp.success else { return }
                var btns: [UIButton] = []
                for (index, item) in models.enumerated() {
                    let btn = self.getBtn(item)
                    self.contentV.addSubview(btn)
                    btn.snp.makeConstraints { m in
                        m.height.equalTo(.scale * 48)
                        m.left.equalTo(.scale * 48)
                        m.right.equalTo(-.scale * 48)
                        m.top.equalTo(.scale * CGFloat(index * (48 + 16)))
                        if index == models.count - 1 {
                            m.bottom.equalToSuperview().offset(-.scale * 24)
                        }
                    }
                    btns.append(btn)
                }
                self.upButtonObservables(btns)
                self.currect = models.first
            }).disposed(by: disposeBag)
        
//        let arr = ["暂无", "初级会计师", "中级会计师", "高级会计师", "注册会计师"]
        
    }
}

class XS_Q3VC: XS_QuestionVC {
    override func upView() {
        super.upView()
        let prve = getPrevious()
        view.addSubview(prve)
        prve.snp.makeConstraints { m in
            m.height.equalTo(.scale*48)
            m.bottom.equalTo(-.scale*32)
            m.left.equalTo(.scale*16)
            m.width.equalTo(.scale*120)
        }
        let next = getNext(XS_QuestionVC.NextDetail(title: "问题3，共3个", text: "您的学习目的是什么？", desc: "请回答下列问题，方便我们为您量身定制课程计划。", next: XS_Q4VC.self))
        view.addSubview(next)
        next.snp.makeConstraints { m in
            m.height.equalTo(.scale*48)
            m.bottom.equalTo(-.scale*32)
            m.left.equalTo(prve.snp.right).offset(.scale*16)
            m.right.equalTo(-.scale*16)
        }
        
        XS_NetWork.requestJson("/dic/queryDicByKey", parameters: ["key":"income"])
            .mapModels(type: XS_SelectIssue.self)
            .subscribe(onNext: { [weak self] models, resp in
                guard let `self` = self, resp.success else { return }
                var btns: [UIButton] = []
                for (index, item) in models.enumerated() {
                    let btn = self.getBtn(item)
                    self.contentV.addSubview(btn)
                    btn.snp.makeConstraints { m in
                        m.height.equalTo(.scale * 48)
                        m.left.equalTo(.scale * 48)
                        m.right.equalTo(-.scale * 48)
                        m.top.equalTo(.scale * CGFloat(index * (48 + 16)))
                        if index == models.count - 1 {
                            m.bottom.equalToSuperview().offset(-.scale * 24)
                        }
                    }
                    btns.append(btn)
                }
                self.upButtonObservables(btns)
                self.currect = models.first
            }).disposed(by: disposeBag)
        
//        let arr = ["1000元以下", "1000元-2999元", "3000元-5999元", "6000元-10000元", "10000元及以上"]
        
    }
}

class XS_Q4VC: XS_QuestionVC {
    override func upView() {
        super.upView()
        let sure = XS_NoHighlightBtn()
//        sure.setTitle("定制专属计划", for: .normal)
        sure.setTitle("开启学习", for: .normal)
        sure.setTitleColor(#colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), for: .normal)
        sure.titleLabel?.font = .xsFont(name: .medium, .scale*16)
        sure.layer.cornerRadius = .scale*24
        sure.layer.backgroundColor = #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)
        view.addSubview(sure)
        sure.snp.makeConstraints { m in
            m.height.equalTo(.scale*48)
            m.bottom.equalTo(-.scale*32)
            m.left.equalTo(.scale*48)
            m.right.equalTo(-.scale*48)
        }
        
        sure.rx.tap.asDriver()
            .drive(onNext: { [weak self] in
                guard let `self` = self, let currect = self.currect, self.save.count >= 2 else { return }
                XS_NetWork.requestJson("/user/addUserRecord", parameters: [
                    "employmentDirection":self.save[0].industryName,
//                    "professionalLevel":self.save[1].industryName,
//                    "incomeAmount":self.save[2].name,
                    "professionalLevel":"-1",
                    "incomeAmount":self.save[1].name,
                    "learnPurpose":currect.name,
                    "industryId": self.save[0].id
                ]).subscribe(onNext: { [weak self] resp in
                    guard let `self` = self else { return}
                    let models = XS_Singleton.shared.infoModel
                    models?.isAnswer = 1
                    XS_Singleton.shared.infoModel = models
//                    let vc = XS_ExclusivePlanVC()
//                    vc.industryId = self.save[0].id
//                    self.navigationController?.pushViewController(vc, animated: true)
                    self.navigationController?.popToRootViewController(animated: true)
                }).disposed(by: self.disposeBag)
            }).disposed(by: disposeBag)
        
        XS_NetWork.requestJson("/dic/queryDicByKey", parameters: ["key":"goal"])
            .mapModels(type: XS_SelectIssue.self)
            .subscribe(onNext: { [weak self] models, resp in
                guard let `self` = self, resp.success else { return }
                var btns: [UIButton] = []
                for (index, item) in models.enumerated() {
                    let btn = self.getBtn(item)
                    self.contentV.addSubview(btn)
                    btn.snp.makeConstraints { m in
                        m.height.equalTo(.scale * 48)
                        m.left.equalTo(.scale * 48)
                        m.right.equalTo(-.scale * 48)
                        m.top.equalTo(.scale * CGFloat(index * (48 + 16)))
                        if index == models.count - 1 {
                            m.bottom.equalToSuperview().offset(-.scale * 24)
                        }
                    }
                    btns.append(btn)
                }
                self.upButtonObservables(btns)
                self.currect = models.first
            }).disposed(by: disposeBag)
        
//        let arr = ["求职就业", "技能提升", "工资翻倍"]
    }
}
